package cn.yitun.chat.yitunserverimapicore.business.contact.service.impl;

import cn.yitun.chat.yituncommonmessage.ExceptionEnum;
import cn.yitun.chat.yituncommonmessage.result.ResultMessage;
import cn.yitun.chat.yitunservercommonlib.crud.yitun.CrudServiceImpl;
import cn.yitun.chat.yitunservercommonlib.dataentity.dto.contact.ContactRelationDto;
import cn.yitun.chat.yitunservercommonlib.dataentity.po.contact.ContactRelationInfo;
import cn.yitun.chat.yitunservercommonlib.dataentity.vo.contact.ContactRelationVo;
import cn.yitun.chat.yitunservercommonlib.enums.userChat.RelationBlockEnums;
import cn.yitun.chat.yitunservercommonlib.util.R;
import cn.yitun.chat.yitunserverimapicore.business.contact.service.ContactRelationService;
import cn.yitun.chat.yitunserverimbase.dao.contact.ContactRelationDao;
import cn.yitun.chat.yitunserverimcore.general.main.push.contact.ContactPush;
import cn.yitun.chat.yitunserverimcore.general.main.push.contact.ContactRelationPush;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 
 * Date 2019-01-20 14:15:38<br>
 * Description
 *
 * @author yitun<br>
 * @version 1.1
 */
@Service
@Transactional
public class ContactRelationServiceImpl
		extends CrudServiceImpl<ContactRelationDao, ContactRelationDto, ContactRelationInfo, ContactRelationVo>
		implements ContactRelationService {

	@Resource
	private ContactRelationPush contactRelationPush;
	@Resource
	private ContactPush contactPush;


	/**
	 * Date 2019-01-20 17:31:47<br>
	 * Description 获取所属用户的联系人分组列表
	 * @param ownerUserId
	 * @return
	 * @author yitun<br>
	 * @version 1.1
	 */
	public List<ContactRelationVo> getListByOwnerUserId(Long ownerUserId) {
		List<ContactRelationVo> list = dao.listVO(R.param("ownerUserId", ownerUserId));
		return list;
	}

	/**
	 * 联系人 计数
	 * @param ownerUserId
	 * @return
	 */
	@Override
	public int countByOwnerUserId(Long ownerUserId) {
		return dao.count(R.param("ownerUserId", ownerUserId));
	}

	public ContactRelationVo getByContactUserId(ContactRelationDto query) {
		return dao.getVOByContactUserId(query.getOwnerUserId(), query.getContactUserId());
	}

	public List<ContactRelationVo> getListByContactUserId(Long ownerUserId, Long contactUserId) {
		return dao.listVO(
				R.param("ownerUserId", ownerUserId)
				.put("contactUserId", contactUserId));
	}

	/**
	 * 
	 * Date 2019-01-20 14:15:50<br>
	 * Description 移动好友到其它分组
	 * 
	 * @param query
	 * @return
	 * @author yitun<br>
	 * @version 1.1
	 */
	public ResultMessage moveCategory(Long key, ContactRelationDto query) {

		Long ownerUserId = query.getOwnerUserId();
		List<Long> contactUserIds = query.getContactUserIds();
		Long categoryId = query.getCategoryId();

		ResultMessage message = new ResultMessage();

		int count = dao.updateCategoryId(ownerUserId, contactUserIds, categoryId);
		if (count <= 0) {
			message.addWarning(ExceptionEnum.SOCK_CONTACT_CATEGORY_MOVE_FAILED);
		} else {
			contactRelationPush.pushMoveCategory(key, contactUserIds, categoryId, ownerUserId);
		}
		return message;
	}

	/**
	 * 
	 * Date 2019-01-20 14:17:03<br>
	 * Description 修改好友备注
	 * 
	 * @param key
	 * @param query
	 * @return
	 * @author yitun<br>
	 * @version 1.1
	 */
	public ResultMessage updateRemark(Long key, ContactRelationDto query) {
		Long ownerUserId = query.getOwnerUserId();
		Long contactUserId = query.getContactUserId();
		String remark = query.getRemark();
		ResultMessage message = new ResultMessage();
		try {

			if (contactUserId != null) {
				int count = dao.updateRemark(ownerUserId, contactUserId, remark);
				if (count <= 0) {
					message.addWarning(ExceptionEnum.DO_FAIL);
				} else {
					contactRelationPush.pushUpdateRemark(key, contactUserId, remark, ownerUserId);
				}
			} else {
				message.addWarning(ExceptionEnum.BUSY_SERVER);
			}
		} catch (Exception e) {
			message.addWarning(ExceptionEnum.BUSY_SERVER);
		}
		return message;
	}

	/**
	 * 
	 * Date 2019-01-20 14:18:20<br>
	 * Description 删除好友
	 * 
	 * @param query
	 * @return
	 * @author yitun<br>
	 * @version 1.1
	 */
	public ResultMessage delete(Long key, ContactRelationDto query) {

		Long ownerUserId = query.getOwnerUserId();
		Long contactUserId = query.getContactUserId();

		ResultMessage message = new ResultMessage();
		int count = dao.deleteByContactUserId(ownerUserId, contactUserId);
		if (count <= 0) {
			message.addWarning(ExceptionEnum.DO_FAIL);
		} else {
			contactRelationPush.pushDelete(key, contactUserId, ownerUserId);
		}
		return message;
	}

	public ResultMessage updateBlocked(Long key, ContactRelationDto query) {

		Long ownerUserId = query.getOwnerUserId();
		Long contactUserId = query.getContactUserId();
		String isBlocked = query.getIsBlocked();

		ResultMessage message = new ResultMessage();

		int count = dao.updateIsBlocked(ownerUserId, contactUserId, isBlocked);
		if (count <= 0) {
			message.addWarning(ExceptionEnum.DO_FAIL);
		} else {

			// 删除在对方好友列表
			count = dao.deleteByContactUserId(contactUserId, ownerUserId);
			contactRelationPush.pushUpdateBlocked(key, contactUserId, isBlocked, ownerUserId);
			if (RelationBlockEnums.BLOCK_YES.getType().equals(isBlocked)) {
				contactPush.pushUpdateBlocked(key, ownerUserId, isBlocked, contactUserId);
			}
		}
		return message;
	}

	public ContactRelationVo getVOById(Long ownerUserId, Long relationId) {
		return dao.getVO(relationId);
	}
}
